package com.javaking.clanteam.studentutils.courses;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.google.ads.AdView;
import com.javaking.clanteam.studentutils.MainActivity;
import com.javaking.clanteam.studentutils.R;
import com.javaking.clanteam.studentutils.SettingsActivity;

public class CoursesActivity extends SherlockFragmentActivity {
	
	Fragment mCurrentFragment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_courses);
		
		getSupportFragmentManager().beginTransaction()
				.add(R.id.fragmentContainer, new CourseListFragment()).commit();
		
		MainActivity.checkForAds(this);
		
		final View activityRootView = findViewById(R.id.activityRoot);
		activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
		  @Override
		  public void onGlobalLayout() {
			  int keyboardHidden = getResources().getConfiguration().keyboardHidden;
			  if (keyboardHidden == Configuration.KEYBOARDHIDDEN_NO) {
				  AdView adView = (AdView) findViewById(R.id.ad);
				  adView.stopLoading();
				  adView.setVisibility(View.GONE);
			  } else if (keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
				  MainActivity.checkForAds(CoursesActivity.this);
			  }
		  }
		});
	}
	
	/**
	 * @see android.app.FragmentActivity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		MainActivity.checkForAds(this);
	}
	
	/**
	 * @see android.app.FragmentActivity#onAttachFragment(android.support.v4.app.Fragment)
	 */
	@Override
	public void onAttachFragment(Fragment fragment) {
		super.onAttachFragment(fragment);
		
		mCurrentFragment = fragment;
		
		MainActivity.checkForAds(this);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(R.string.menu_settings).setIcon(android.R.drawable.ic_menu_preferences)
		.setIntent(new Intent(this,SettingsActivity.class));
		getSupportMenuInflater().inflate(R.menu.activity_courses, menu);
    return true;
	}

}
